<?php


namespace common\services;


use common\helpers\Util;

class QiyeSmsService
{
    /**
     * 企业宝发送短信
     * @param $mobile
     * @param string $content
     * @return bool
     * @throws \Exception
     */
    public function sendSms($mobile, $content)
    {

        $resultObj = $this->sendMessage($mobile, $content);
        if ($resultObj->respcode == 0) {
            return true;
        } else {
            \Yii::info("短信发送失败:" . $resultObj->respdesc,"appInfo");
            return false;
        }
    }

    /**
     * 短信发送
     * @param $phone
     * @param $content
     * @param string $sendtime
     * @param string $port
     * @param string $needstatus
     * @return mixed
     * @throws \Exception
     */
    private function sendMessage($phone, $content, $sendtime = '', $port = '', $needstatus = '')
    {
        $params = \Yii::$app->params;
        $username = $params['qiye_sms']['username'] ?? null;
        $password = $params['qiye_sms']['password'] ?? null;
        $sms_sign = $params['qiye_sms']['sms_sign'] ?? null;
        $sms_url = $params['qiye_sms']['sms_url'] ?? null;
        if (empty($username) || empty($password) || empty($sms_sign) || empty($sms_url)) {
            throw new \Exception('短信发送失败，请确认短信配置信息');
        }
        $msg = $sms_sign . $content;
        $ch = curl_init();
        $post_data = "username=" . $username . "&passwd=" . $password . "&phone=" . $phone . "&msg=" . urlencode($msg) . "&needstatus=true&port=" . $port . "&sendtime=" . $sendtime;
        curl_setopt($ch, CURLOPT_URL, $sms_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $file_contents = curl_exec($ch);
        curl_close($ch);
        return json_decode($file_contents);
    }


    /**
     * 短信余额查询
     * @return mixed
     * @throws \Exception
     */
    public function getAccountBalance()
    {
        $params = \Yii::$app->params;
        $username = $params['qiye_sms']['username'] ?? null;
        $password = $params['qiye_sms']['password'] ?? null;
        $sms_url = $params['qiye_sms']['sms_get_balance_url'] ?? null;
        if (empty($username) || empty($password) || empty($sms_url)) {
            throw new \Exception('短信余额查询失败，请检查配置');
        }
        $timestamp = time() * 1000;
        $sign = md5($username.$password.$timestamp);
        $ch = curl_init();
        $post_data = "account=" . $username . "&signature=" . $sign . "&timestamp=" . $timestamp;
        curl_setopt($ch, CURLOPT_URL, $sms_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $file_contents = curl_exec($ch);
        curl_close($ch);
        return json_decode($file_contents);
    }


}